package messageHandler;

import screen.BauCuaScr;
import network.Cmd_Message;
import network.Message;

public class GameBauCuaMsgHandler extends GameCenterMsgHandler{
	static GameBauCuaMsgHandler m_instance = null;
	
    public static GameBauCuaMsgHandler gI() {
        if (m_instance == null) {
            m_instance = new GameBauCuaMsgHandler();
        }
        return m_instance;
    }
	
	public static void onHandler() {		
		GlobalMsgHandler.gI().miniGameMessageHandler = GameBauCuaMsgHandler.gI();			
	}
	public void onMessage(Message msg) {
		// TODO Auto-generated method stub
		
		try {
            switch (msg.command) {
            case Cmd_Message.START:
            	short roomID = msg.reader().readByte();
            	short boardID = msg.reader().readByte();
            	short interval = msg.reader().readByte();
            	BauCuaScr.gI().onStartGame(interval);
            	break;
            	
            case Cmd_Message.END_GAME:
            	roomID = msg.reader().readByte();
            	boardID = msg.reader().readByte();
            	boolean isBreak = msg.reader().readBoolean();
            	byte num1 = 0, num2 = 0, num3 = 0;
            	String des = "";
            	if(!isBreak)
            	{
            		num1 = msg.reader().readByte();
            		num2 = msg.reader().readByte();
            		num3 = msg.reader().readByte();
            		des = msg.reader().readUTF();
            	}
            	BauCuaScr.gI().onEndGame(isBreak, num1, num2, num3, des);
            	break;
            	
            case Cmd_Message.BEGIN_QUEST://bat dau lac bau cua
            	roomID = msg.reader().readByte();
            	boardID = msg.reader().readByte();
            	BauCuaScr.gI().onLacBauCua();
            	break;
            case Cmd_Message.SUBMIT_DATA:
            	roomID = msg.reader().readByte();
            	boardID = msg.reader().readByte();
            	int playerId = msg.reader().readInt();
            	boolean finish = msg.reader().readBoolean();
            	byte[] arrFace = new byte[3];
            	byte[] arrData = new byte[3];
            	int i;
            	for(i = 0; i < 3; i++){
            		arrFace[i] = -1;
            		arrData[i] = -1;
            	}
            	i = 0;
            	while(msg.reader().available() > 0){
            		arrFace[i] = msg.reader().readByte();
            		arrData[i] = msg.reader().readByte();
            		i++;
            		if(i == 3)
            			break;
            	}
            	BauCuaScr.gI().onReceiveBetData(playerId, arrFace, arrData, finish);
            	break;
        
            	           	
            default:
            	super.onMessage(msg);
            	break;
            }
		}catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
	}
}
